package cn.tansanqinger.controller.announcement;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tansanqinger.service.announcement.IAnnouncementService;
import cn.tansanqinger.util.PageData;

@Controller
@RequestMapping("/admin/announcement")
public class AnnouncementMessageController {
	
	@Autowired
	private IAnnouncementService service;

	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, ModelAndView mv) {
		
		mv.setViewName("/announcement/list");
		return mv;
	}
	
	@RequestMapping("/findListData")
	@ResponseBody
	public String findListData(HttpServletRequest request) {
		try {
			return new ObjectMapper().writeValueAsString(service.selectAnnouncement(request));
		} catch (Exception e) {}
		return null;
	}
	
	/**
	 * 添加信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/goAdd")
	public ModelAndView goAdd(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("serialNumber", service.announcementNumber(request));
		mv.setViewName("/announcement/goAdd");
		return mv;
	}
	
	/**
	 * 编辑信息
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request, ModelAndView mv) {
		mv.addObject("announcement", service.selectAnnouncementOne(request));
		mv.setViewName("/announcement/edit");
		return mv;
	}
	
	/**
	 * 添加数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public PageData add(HttpServletRequest request) {
		return service.add(request);
	}
	
	/**
	 * 数据删除
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public PageData delete(HttpServletRequest request){
		return service.delete(request);
	}
	
	/**
	 * 修改信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/modification")
	@ResponseBody
	public PageData modification(HttpServletRequest request){
		return service.modification(request);
	}
}
